package com.example.dormitorysystem.service.impl;

import com.example.dormitorysystem.entity.*;
import com.example.dormitorysystem.entity.Class;
import com.example.dormitorysystem.entity.pojo.Roommate;
import com.example.dormitorysystem.exception.*;
import com.example.dormitorysystem.mapper.*;
import com.example.dormitorysystem.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-13
 */

@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Resource
    private AuthMapper authMapper;

    @Resource
    private BedsMapper bedsMapper;

    @Resource
    private RoomsMapper roomsMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Resource
    private ClassMapper classMapper;


    @Override
    public Map<String, Object> getUserInfo(HttpServletRequest request, HttpServletResponse response) {
        Integer uid = Integer.parseInt(request.getAttribute("uid").toString());

        Users user = usersMapper.selectById(uid);
        StudentInfo studentInfo = studentInfoMapper.selectByUid(uid);
        Class aClass = classMapper.selectById(studentInfo.getClassId());
        if (user == null || studentInfo == null || aClass == null) {
            throw new CmsException(401, "未找到您的信息！");
        }

        // 返回数据
        Map<String, Object> res = new HashMap<>();
        res.put("uid", uid);
        res.put("studentid", studentInfo.getStudentid());
        res.put("name", user.getName());
        res.put("gender", user.getGender());
        res.put("email", user.getEmail());
        res.put("tel", user.getTel());
        res.put("last_login_time", user.getLastLoginTime());
        res.put("verification_code", studentInfo.getVerificationCode());
        res.put("class_name", aClass.getName());
        return res;
    }

    @Override
    public void changePasswd(Map<String, Object> input, HttpServletRequest request) {
        String oldPasswd = (String) input.get("oldPasswd");
        String newPasswd = (String) input.get("newPasswd");
        Integer uid = Integer.parseInt(request.getAttribute("uid").toString());
        String username = request.getAttribute("username").toString();
        Auth auth = authMapper.selectByUidAndName(uid, username);
        if (auth == null) {
            throw new CmsException(ErrorCode.SESSION_IS_EXPIRED);
        }

        System.out.println(oldPasswd + " " + newPasswd);
        if (oldPasswd == null || newPasswd == null) {
            throw new CmsException(ErrorCode.INCOMPLETE_PARAMETER);
        }

        if (!oldPasswd.equals(auth.getPassword())) {
            throw new CmsException(ErrorCode.OLD_PASSWORD_IS_WRONG);
        }
        //直接修改，没有加密
        auth.setPassword(newPasswd);
        authMapper.updateById(auth);
    }


    @Override
    public Map<String, Object> getMyroomInfo(HttpServletRequest request, HttpServletResponse response) {

        Integer id = Integer.parseInt(request.getAttribute("uid").toString());
        Beds beds = bedsMapper.selectByUid(id);
        if (beds == null) {
            throw new CmsException(400, "暂无你的宿舍信息！");
        }
        Rooms rooms = roomsMapper.selectById(beds.getRoomId());
        // 返回数据
        Map<String, Object> res = new HashMap<>();
        res.put("roomName", rooms.getName());
        res.put("roomId", rooms.getId());

        List<String> roommates = bedsMapper.selectListByRoomId(rooms.getId());

        LinkedList<Roommate> roommates1 = new LinkedList<>();
        for (String name : roommates) {
            Roommate roommate = new Roommate();
            roommate.setName(name);
            roommates1.add(roommate);
        }
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("rows", roommates1);
        res.put("member", stringObjectHashMap);
        return res;
    }


}
